/**
 *  X87Compiler.java : IceBird project
 *  17:34:45 
 */
package icebird.compiler.ncomp;

import icebird.compiler.ncomp.vs.*;
import icebird.metadata.BasicType;

/**
 * Represents x87 FPU compiler.
 * @author Sergey Shulepoff[Knott]
 */
public class X87Compiler extends FPCompiler {
	
	private VirtualStack getStack() {
		return code.getVS();
	}

	public X87Compiler(CodeCompiler c) {
		super(c);
	}
	
	private double getValue(Item i) {
		if (i.getType() == BasicType.Double) {
			return ((DoubleConstantLocation)((DoubleItem)i).getLocation()).getValue();
		} else {
			return ((FloatConstantLocation)((FloatItem)i).getLocation()).getValue();
		}
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#add(icebird.metadata.BasicType)
	 */
	@Override
	public void add(BasicType type) {
		VirtualStack vs = getStack();
		
		Item v2 = vs.pop();
		Item v1 = vs.pop();
		
		// Both constants.
		if (v1.getLocation().isConstant() && v2.getLocation().isConstant()) {
			double value = getValue(v1) + getValue(v2);
			
			if (type.isWide()) {
				vs.push(new DoubleItem(ConstLocation.createDConst(value)));
			} else {
				vs.push(new FloatItem(ConstLocation.createFConst((float)value)));
			}
			
			v1.release();
			v2.release();
		} else {
			
		}
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#compare(boolean, icebird.metadata.BasicType)
	 */
	@Override
	public void compare(boolean gt, BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#convert(icebird.metadata.BasicType, icebird.metadata.BasicType)
	 */
	@Override
	public void convert(BasicType fromType, BasicType toType) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#div(icebird.metadata.BasicType)
	 */
	@Override
	public void div(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#fpaload(icebird.metadata.BasicType)
	 */
	@Override
	public void fpaload(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#mul(icebird.metadata.BasicType)
	 */
	@Override
	public void mul(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#neg(icebird.metadata.BasicType)
	 */
	@Override
	public void neg(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#rem(icebird.metadata.BasicType)
	 */
	@Override
	public void rem(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.ncomp.FPCompiler#sub(icebird.metadata.BasicType)
	 */
	@Override
	public void sub(BasicType type) {
		// TODO Auto-generated method stub
		
	}

	
	
}
